<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/10/30
 * Time: 14:05
 */
namespace Home\Controller;
use Think\Controller;

class TimedTaskController extends CommonController
{
    /*
     * 确认凭证定时方法
     */
 public function douniu_certificate()
 {
     $certificate = M('certificate', '', 'DB2')->where(['is_delete' =>1, 'status' =>['in','2,3,4'],'is_douniu'=>0,'shop_identifier'=>['neq','0100010020']])->limit(3)->field('id,borrower_id,lender_id,money,interest,numbering,repayment_time,loan_time')->select();
     foreach ($certificate as $key => $value) {
         //$is_nn_order=M('nn_order','','DB2')->where(['numbering'=>$value['numbering']])->getField('id');
         //if(empty($is_nn_order)){
         //$user_mobb = M('certificate', Null, 'DB2')->where(["numbering" => $certificate["certificate_no"], "status" => 1, "lender_id=" . $certificate['user_id'] . " or borrower_id=" . $certificate['user_id'] . ""])->find();
         $lender_data = M('lender', '', C('DB2'))->where(['id' => $value['lender_id']])->field('name,mobile,idcard')->find();
         $boorower_data = M('user', '', C('DB2'))->where(['id' => $value['borrower_id']])->field('username,identity_num,mobile')->find();
         $postdata['boorowMobile'] = $boorower_data['mobile'];
         $postdata['borrowName'] = $boorower_data['username'];
         $postdata['borrowIdentity'] = $boorower_data['identity_num'];
         $postdata['lenderMobile'] = $lender_data['mobile'];
         $postdata['lenderName'] = $lender_data['name'];
         $postdata['lenderIdentity'] = $lender_data['idcard'];
         $postdata['borrowMoney'] = $value["money"];
         $postdata['interest'] = $value['interest'] - $value['money'];
         $postdata['returnTimeStr'] = $value["repayment_time"] . '000';
         $postdata['borrowTimeStr'] = $value['loan_time'] . '000';
         $postdata['lenderEmail'] = '';
         $postdata['borrowEmail'] = '';
         $postdata['purpose'] = '日常消费';
         $postdata['borrowNote'] = $value["numbering"];
         $url = 'http://www.hzdouniu.cn/api/xyc/save_lous';
         $res = $this->get_curl($url, $postdata);
         $res = json_decode($res, true);
         if ($res['resultCode'] == '1000') {
             $add = array(
                 'borrower_id' => $value['borrower_id'],
                 'lender_id' => $value['lender_id'],
                 'numbering' => $value["numbering"],
                 'nn_orderno' => $res['data']['orderNo'],
                 'status' => '1',
                 'ctime' => time(),
             );
             $nn_order=M('nn_order', '', C('DB2'))->where(['numbering'=>$value['numbering']])->getField('id');
             if($nn_order){
                 M('nn_order', '', C('DB2'))->save($add);
             }else{
                 M('nn_order', '', C('DB2'))->add($add);
             }
             M('certificate', '', C('DB2'))->where(['numbering' => $value['numbering']])->setField('is_douniu', '1');
         }
         else {
             $add = array(
                 'borrower_id' => $value['borrower_id'],
                 'lender_id' => $value['lender_id'],
                 'numbering' => $value["numbering"],
                 'nn_orderno' => 'error',
                 'status' => '1',
                 'ctime' => time(),
                 'err_msg' => $res['error'],
             );
             $nn_order=M('nn_order', '', C('DB2'))->where(['numbering'=>$value['numbering']])->getField('id');
             if($nn_order){
                 M('nn_order', '', C('DB2'))->save($add);
             }else{
                 M('nn_order', '', C('DB2'))->add($add);
             }
         }
       }
   //  }
 }
 /*
  * 展期确认凭证定时方法
  */
    public function douniu_extension(){
        $certificate_data = M('certificate_data','',C('DB2'))->where(['status'=>4,'is_douniu'=>1,'shop_identifier'=>['neq','0100010020']])->limit(3)->field('numbering,lender_id,mobile,money,interest,repayment_time,period_time')->select();
        foreach ($certificate_data as $key=>$value) {
            $lender = M('lender', '', 'DB2')->where(['id' => $value['lender_id']])->getField('mobile');
            $postdata['lenderMobile']=$lender;
            $postdata['borrowMobile']=$value['mobile'];
            $postdata['borrowMoney']=$value['money'];
            $postdata['interest']=$value['interest']-$value['money'];
            $postdata['expireTime']=$value['repayment_time'];
            $postdata['augmentTime']=$value["period_time"];
            $postdata['augmentNote']=$value['numbering'];
            $order = M('nn_order', '', C('DB2'))->where(['numbering' => $value['numbering']])->getField('nn_orderno');
            $postdata['orderNo'] = $order;
            $url = 'http://www.hzdouniu.cn/api/xyc/save_agument';
            $res = $this->get_curl($url, $postdata);
            $res = json_decode($res, true);
            if ($res['resultCode'] == '1000') {
                M('nn_order', '', C('DB2'))->where(['numbering' => $value['numbering']])->setField('status', '3');
            }
        }
    }
    /*
   * 凭证撕毁定时方法
   */
    public function douniu_tear(){

        $certificate_data = M('certificate_data','',C('DB2'))->where(['status'=>6,'is_douniu'=>1,'shop_identifier'=>['neq','0100010020']])->limit(3)->field('numbering,lender_id,real_repayment_time')->select();
        foreach ($certificate_data as $key=>$value) {
            $lender = M('lender', '', 'DB2')->where(['id'=>$value['lender_id']])->getField('mobile');
            $postdata['lenderMobile'] = $lender;
            $order = M('nn_order', '', C('DB2'))->where(['numbering' =>$value['numbering']])->getField('nn_orderno');
            $postdata['orderNo'] = $order;
            $postdata['tearupTime'] =$value['real_repayment_time'];
            $url = 'http://www.hzdouniu.cn/api/xyc/tear_up_voucher';
            $res = $this->get_curl($url, $postdata);
            $res = json_decode($res, true);
            if ($res['resultCode'] == '1000') {
                M('nn_order', '', C('DB2'))->where(['numbering' => $value['numbering']])->setField('status', '4');
            }
        }
    }

    public function get_curl($url = '',$data='',$header='')
    {

        $ch = curl_init();
        if (!empty($header)) {
//            $header=array('Content-type: application/json','charset:utf-8'); 示例
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        }
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, '1');
        curl_setopt($ch, CURLOPT_URL, $url);
        if (!empty($data)) {
            curl_setopt($ch, CURLOPT_POST, '1');
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        }
        //禁用后cURL将终止从服务端进行验证
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        $res = curl_exec($ch);
        if (curl_error($ch)) {
            return '错误信息:' . curl_error($ch);
        }
        curl_close($ch);
        return $res;
    }
}